#pragma once
#include "MallocBase.h"
#include "../Utils/PlatTypeUtil.h"


struct MallocPool;

class MallocCrash : public MallocBase
{
	friend struct PoolDesc;
	friend struct MallocPool;
public:

	enum
	{
		REQUIRED_ALIGNMENT = 16,
		PER_ALLOC_OVERHEAD = sizeof(PtrInfo),

		NUM_POOLS = 14,
		MAX_NUM_ALLOCS_IN_POOL = 2048,

		MEM_TAG = 0xfe,
		MEM_WIPETAG = 0xcd,
	};

	MallocCrash();
	virtual ~MallocCrash();

	virtual void* Malloc(uint32 count, uint32 alignment = DEFAULT_ALIGNMENT) override;


	virtual void* Realloc(void* original, uint32 count, uint32 alignment = DEFAULT_ALIGNMENT) override;


	virtual void Free(void* original) override;


	virtual bool GetAllocationSize(void* original, uint32& sizeOut) override;

protected:

	const PoolDesc& GetPoolDesc(uint32 index) const;

	uint32 GetPoolTotalSize() const;

	MallocPool* FindPoolFromSize(uint32 allocationSize) const;

	uint8* AllocateFromPool(uint32 allocationSize);

	static uint32 GetAllocationSize(void* original);

	bool IsPtrInPool(void* ptr);

protected:

	/** Current position in the small memory pool. */
	uint32 mMemoryPoolOffset;

	/** Preallocated memory pool for small allocations. */
	uint8* mMemoryPool;

	

};

